﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model {
	public class TreeNode {
		#region 根据DataTable生成Json树结构
		/// <summary>
		/// 根据DataTable生成Json树结构
		/// </summary>
		/// <param name="tabel">数据源</param>
		/// <param name="idCol">ID列</param>
		/// <param name="txtCol">Text列</param>
		/// <param name="rela">关系字段</param>
		/// <param name="pId">父节点的值</param>
		///结果字符串
		StringBuilder result = new StringBuilder();
		StringBuilder sb = new StringBuilder();
		public string GetTreeJsonByTable(DataTable tabel, string IdCol, string LimitNameCol, string ParentIdCol, int PId) {
			result.Append(sb.ToString());
			sb.Clear();
			if (tabel.Rows.Count > 0) {
				sb.Append("[");
				string filer = string.Format("{0}='{1}'", ParentIdCol, PId);
				DataRow[] rows = tabel.Select(filer);
				if (rows.Length > 0) {
					foreach (DataRow row in rows) {
						if (row["Jurisdiction_url"] != null || row["Jurisdiction_url"].ToString() != "") {
							sb.Append("{\"id\":\"" + row[IdCol] + "\",\"text\":\"" + row[LimitNameCol] + "\", \"iconCls\":\"tree-join\",\"state\":\"open\",\"attributes\":{\"url\":\"" + row["Jurisdiction_url"] + "\"}");
						}
						else {
							sb.Append("{\"id\":\"" + row[IdCol] + "\",\"text\":\"" + row[LimitNameCol] + "\",\"iconCls\":\"icon-ok\",\"state\":\"open\"");
						}
						if (tabel.Select(string.Format("{0}='{1}'", ParentIdCol, row[IdCol])).Length > 0) {


							sb.Append(",\"children\":");
							GetTreeJsonByTable(tabel, IdCol, LimitNameCol, ParentIdCol, Convert.ToInt32(row[IdCol]));
							result.Append(sb.ToString());
							sb.Clear();
						}
						result.Append(sb.ToString());
						sb.Clear();
						sb.Append("},");
					}
					sb = sb.Remove(sb.Length - 1, 1);
				}
				sb.Append("]");
				result.Append(sb.ToString());
				sb.Clear();
			}
			return result.ToString();
			//return Json(result, JsonRequestBehavior.AllowGet);
		}
		#endregion

		public string GetTreeJsonByList(List<SysAuthorityAndRoles> lis, int pId) {
			//创建集合
			var rows = lis.Where<SysAuthorityAndRoles>(a => a.ParentID == pId);
			List<SysAuthorityAndRoles> lis1 = lis;
			result.Append(sb.ToString());
			sb.Clear();
			sb.Append("[");
			foreach (var item in rows) {
				if (item.Authority_Url != "") {
					sb.Append("{\"id\":\"" + item.ID + "\",\"text\":\"" + item.Authority_Name + "\",\"state\":\"open\",\"attributes\":{\"url\":\"" + item.Authority_Url + "\"}");
				}
				else {
					sb.Append("{\"id\":\"" + item.ID + "\",\"text\":\"" + item.Authority_Name + "\",\"state\":\"open\"");
				}
				var lis2 = lis.Where<SysAuthorityAndRoles>(a => a.ParentID == item.ID);
				if (lis2.Count() > 0) {
					sb.Append(",\"children\":");
					GetTreeJsonByList(lis1, item.ID);
					result.Append(sb.ToString());
					sb.Clear();
				}
				result.Append(sb.ToString());
				sb.Clear();
				sb.Append("},");
			}
			sb = sb.Remove(sb.Length - 1, 1);
			sb.Append("]");
			result.Append(sb.ToString());
			sb.Clear();
			string aa = result.ToString();
			return result.ToString();
		}
	}
}
